package com.SouthernWall_404.AgeRight.Common.API.MissionAPI;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public abstract class AbstractMissionInfo {

    // TagKey 常量
    private static final String TAG_ROOT = "age_right";
    private static final String TAG_MISSION = "mission";
    private static final String TAG_TYPE = "type";
    private static final String TAG_PROGRESS = "progress";
    private static final String TAG_GOAL = "goal";
    private static final String TAG_DESCRIBE = "describe";
    private static final String TAG_MISSION_CONTENT_PACK = "mission_content_pack";

    public MissionType type;
    private int progress = 0;
    private int goal = 1;
    public float percentage = 0f;
    public boolean isFininshed = false;
    public String describe ="";

    public AbstractMissionInfo(int goal, MissionType type,String describe) {
        this.goal = goal;
        this.type = type;
        this.describe=describe;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public int getProgress() {
        return progress;
    }

    public int getGoal() {
        return goal;
    }

    public void progressOn() {
        this.setProgress(progress + 1);
    }

    public void progressBack() {
        if (progress > 0) {
            this.setProgress(progress - 1);
        }
    }

    public void setGoal(int goal) {
        this.goal = goal;
    }

    public float getPercentage() {
        if (this.progress <= this.goal) {
            percentage = (float) this.progress / goal;
        } else percentage = 1;
        return percentage;
    }

    public void check(Player player) {
        checkFinish();
    }

    public void checkKill(LivingEntity livingEntity, Player player) {
        if (type != MissionType.KILL) {
            return;
        }
    }

    public void checkPosition(BlockPos blockPos, Player player) {
        if (type != MissionType.POSITION) {
            return;
        }
    }

    public void checkCollect(ItemStack itemStack, Player player) {
        if (type != MissionType.COLLECT) {
            return;
        }
    }

    public void checkFinish() {
        if (getPercentage() >= 1) {
            isFininshed = true;
        }
    }

    /**
     * 将任务信息保存到 CompoundTag 中
     * 结构: age_right.mission.任务键名
     */
    public CompoundTag getModPack() {
        CompoundTag missionTag = new CompoundTag();

        // 保存基础数据
        missionTag.putString(TAG_TYPE, type.name());
        missionTag.putString(TAG_DESCRIBE,describe);
        missionTag.putInt(TAG_PROGRESS, progress);
        missionTag.putInt(TAG_GOAL, goal);

        // 为子类提供保存任务内容的空间
        CompoundTag missionContentPack = saveMissionContentPack();
        if (missionContentPack != null && !missionContentPack.isEmpty()) {
            missionTag.put(TAG_MISSION_CONTENT_PACK, missionContentPack);
        }

        // 创建 age_right.mission.xxx 结构
        CompoundTag rootTag = new CompoundTag();
        CompoundTag ageRightTag = new CompoundTag();
        CompoundTag missionDataTag = new CompoundTag();

        missionDataTag.put(getMissionKey(), missionTag);
        ageRightTag.put(TAG_MISSION, missionDataTag);
        rootTag.put(TAG_ROOT, ageRightTag);

        return rootTag;
    }

    /**
     * 从 CompoundTag 中读取任务信息
     */
    public void loadFromModPack(CompoundTag tag) {
        if (!tag.contains(TAG_ROOT)) return;

        CompoundTag ageRightTag = tag.getCompound(TAG_ROOT);
        if (!ageRightTag.contains(TAG_MISSION)) return;

        CompoundTag missionDataTag = ageRightTag.getCompound(TAG_MISSION);
        String missionKey = getMissionKey();

        if (!missionDataTag.contains(missionKey)) return;

        CompoundTag missionTag = missionDataTag.getCompound(missionKey);

        // 读取基础数据
        if (missionTag.contains(TAG_TYPE)) {
            try {
                this.type = MissionType.valueOf(missionTag.getString(TAG_TYPE));
            } catch (IllegalArgumentException ignored) {}
        }
        if (missionTag.contains(TAG_DESCRIBE)) {
            try {
                this.describe = missionTag.getString(TAG_DESCRIBE);
            } catch (IllegalArgumentException ignored) {}
        }

        if (missionTag.contains(TAG_PROGRESS)) {
            this.progress = missionTag.getInt(TAG_PROGRESS);
        }

        if (missionTag.contains(TAG_GOAL)) {
            this.goal = missionTag.getInt(TAG_GOAL);
        }

        // 为子类提供加载任务内容的机会
        if (missionTag.contains(TAG_MISSION_CONTENT_PACK)) {
            loadMissionContentPack(missionTag.getCompound(TAG_MISSION_CONTENT_PACK));
        }

        // 重新计算推导数据
        getPercentage();
        checkFinish();
    }


    /*
    用于获取描述
     */
    public Component getDescribe() {
        return Component.translatable(describe);
    }

    /**
     * 生成任务的唯一键名
     */
    protected String getMissionKey() {
        return this.getClass().getSimpleName().toLowerCase();
    }

    /**
     * 子类可重写此方法以保存任务具体内容
     * 默认返回空，表示没有任务内容需要保存
     *
     * @return 包含任务内容的 CompoundTag
     */
    protected CompoundTag saveMissionContentPack() {
        return new CompoundTag();
    }

    /**
     * 子类可重写此方法以加载任务具体内容
     * 默认实现为空，表示没有任务内容需要加载
     *
     * @param missionContentPack 包含任务内容的 CompoundTag
     */
    protected void loadMissionContentPack(CompoundTag missionContentPack) {
        // 默认实现为空
    }
}